/**
 * 
 */

$(document).ready(function() {
        $("form").submit(function () {
            return false;
        });

        var VAL = {
            NORMAL: "0000000",
            EVT_USER: $("#userId").text(),
            T_FBPBISTOL:"FBPBISTOL",
            FIPINQBOX:"FIPINQBOX",
            FSPWIPBNK:"FSPWIPBNK"

        };
        var controlsQuery = {
                W                 : $(window)              ,
                ppBoxIDInput      : $("#box_id")     ,
                wipBankSelect     : $("#wipBankSelect")    ,
                grdLegendDiv      : $("#grdLegendDiv")     ,
                lotIdInput        : $("#lotIdInput")       ,
                mainGrd   :{
                    grdId     : $("#retPrdAListGrd")   ,
                    grdPgText : "#retPrdAListPg"       ,

                }     
        };
        function initRetPrdAListGrd() {
        	controlsQuery.mainGrd.grdId.jqGrid({
                    url: "",
                    datatype: "local",
                    mtype: "POST",
                    height: 400,
                    width: 1150,
                    // autowidth : true,
                    shrinkToFit : true,
		    		scroll : true,
		    		resizable : true,
		    		rownumbers : true,
		    		loadonce : true,
		            viewrecords : true,
                    multiselect : true,
                    pager: controlsQuery.mainGrd.grdPgText,
                    colModel: [{
                        name: 'box_id',
                        index: 'box_id',
                        label: '卡匣代码',
                        width: 150
                    }, {
                        name: 'box_stat',
                        index: 'box_stat',
                        label: '状态',
                        width: 150
                    }, {
                        name: 'tool_id_fk',
                        index: 'tool_id_fk',
                        label: '在制仓名',
                        width: 150
                    }, {
                        name: 'wo_id_fk',
                        index: 'wo_id_fk',
                        label: '工单号',
                        width: 150
                    }, {
                        name: 'nx_path_id_fk',
                        index: 'NX_PATH_ID_FK',
                        label: '工艺路线',
                        width: 150
                    }, {
                        name: 'nx_ope_id_fk',
                        index: 'NX_OPE_ID_FK',
                        label: '站点信息',
                        width: 150
                    }, {
                        name: 'prd_qty',
                        index: 'PRD_QTY',
                        label: '玻璃数量',
                        width: 150
                    }]
                })
        };
        initRetPrdAListGrd();
    //模态框拖动
    $(".modal-content").draggable();
    //表格自适应
    function resizeFnc(){
        var offsetBottom, grdDivWidth;
        grdDivWidth = $("#divTotal").width();
        offsetBottom = $(window).height() - $("#retPrdAListDiv").offset().top;

        $("#retPrdAListDiv").width(grdDivWidth*0.98);
        $("#retPrdAListDiv").height(offsetBottom*0.99);
        $("#retPrdAListGrd").setGridWidth(grdDivWidth*0.97);
        $("#retPrdAListGrd").setGridHeight(offsetBottom*0.95-51);

    };
    resizeFnc();
    $(window).resize(function() {
        resizeFnc();
    });
        var toolFunc = {

            iniWipBankSelect: function () {
                var inObj,
                    outObj,
                    tool_cnt,
                    tool_ary = [];
                inObj = {
                    trx_id: VAL.T_FBPBISTOL,
                    action_flg: 'Q',
                    iary: [
                        {tool_cate: 'WPBK'}
                    ]
                };
                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code == VAL.NORMAL) {
                    SelectDom.initWithSpace($("#wipBankSelect"));
                    SelectDom.addSelectArr($("#wipBankSelect"), outObj.oary, "tool_id","tool_id","",true);
                }
            },
        };

        /**      * All button click function      * @type {Object}      */
        var btnFunc = {
            //Query
            query_func: function () {
                var inObj,
                    outObj,
                    box_id,
                    tool_id;
                if($("#box_id").val().trim() == ""){
                	return false;
                }
                var rowDatas = $("#retPrdAListGrd").jqGrid("getRowData");
				if(rowDatas.length>0){
					for (var i = 0; i < rowDatas.length; i++) {
						if (rowDatas[i].box_id.trim() == $("#box_id").val().trim() ) {
							$("#box_id").val("");
							return false;
						}
					}
				}
                inObj = {
                    trx_id    : VAL.FIPINQBOX,
                    action_flg: 'C',
                    box_id    : $("#box_id").val()
                };

                outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code == VAL.NORMAL) {
                	if (outObj.box_cnt > 0){
                		
	                	if(outObj.oary_box[0].box_stat.trim() != "WAIT"){
	                		showErrorDialog("1","卡匣状态必须是[WAIT]才可以做此操作！");
	                        return;
	                	}
	                    
	                	var newRowID = getGridNewRowID("#retPrdAListGrd");
						$("#retPrdAListGrd").jqGrid("addRowData",newRowID,outObj.oary_box[0]);
                    } else {
                        showErrorDialog(outObj.box_cnt,"该卡匣不存在！");
                        return;
                    }

                }
            },
            query_wipbank_func : function(){
            	$("#retPrdAListGrd").clearGridData(); 
            	var tool_id = controlsQuery.wipBankSelect.val().trim();
            	if(!tool_id){
            		showErrorDialog("","请选择在制仓名！");
                    return false;
            	}
            	var inObj = {
                    trx_id    : VAL.FIPINQBOX,
                    action_flg: 'C',
                    tool_id: tool_id
                };
            	outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code == VAL.NORMAL) {
                	if (outObj.box_cnt > 0){
                		setGridInfo(outObj.oary_box, "#retPrdAListGrd");
                	}
                }
            },
            //返回产线
            bank_out_func : function(){
                //T_XPWIPBANK
                 var i,
                     inObj,
                     outObj,
                     box_id,
                     crGrid,
                     rowIds,
                     idLen,
                     curRowData,
                     iary = [];
                    crGrid = controlsQuery.mainGrd.grdId;
                    rowIds = crGrid.jqGrid('getGridParam','selarrrow');
                     if(rowIds.length === 0 ){
                         showErrorDialog("","请先勾选需要返回产线的卡匣！");
                         return false;
	                 }
	                 idLen = rowIds.length;
	                 for( i=0; i < idLen; i++){
	                      curRowData = crGrid.jqGrid('getRowData', rowIds[i]);
	                      iary.push({
	                        box_id : curRowData['box_id']
	                      });
	                 }
	                 inObj = {
	                            trx_id     : VAL.FSPWIPBNK ,
	                            action_flg : 'O',
	                            evt_usr    : VAL.EVT_USER,
	                            box_cnt    : idLen       ,
	                            iary       : iary
	                      };
	                 outObj = comTrxSubSendPostJson(inObj);
	                    if (outObj.rtn_code == VAL.NORMAL) {
	                            showSuccessDialog("返回产线成功");
	                            $("#retPrdAListGrd").clearGridData(); 
	                    }
	                 return true;
            },
            //转入在制仓
               bank_in_func : function(){
                   var inObj,
	                   outObj,
	                   box_id,
	                   tool_id,
                   	   iary = new Array();
                   var rowIds = $("#retPrdAListGrd").jqGrid("getGridParam",'selarrrow');
                   if(rowIds.length == 0){
                	   showErrorDialog('',"没有勾选需要转入在制仓的卡匣");
                       return false;
                   }
                   for(var i=0;i<rowIds.length;i++){
                	   var rowData = $("#retPrdAListGrd" ).jqGrid("getRowData",rowIds[i]);
                	   var box_info={
                		   box_id : rowData.box_id
                	   }
                	   iary.push(box_info);
                   }
                   
                   tool_id = controlsQuery.wipBankSelect.val().trim();
                       if(!tool_id){
                       showErrorDialog('','请选择在制仓！');
                       return false;
	                }
	                inObj = {
                         trx_id : VAL.FSPWIPBNK,
                         action_flg : 'I',
                         evt_usr    : VAL.EVT_USER,
                         iary       : iary,
                         tool_id    : tool_id
                    };
                   outObj = comTrxSubSendPostJson(inObj);
                   if (outObj.rtn_code == VAL.NORMAL) {
                           showSuccessDialog("转入在制仓成功");
                           $("#retPrdAListGrd").clearGridData();
//                         btnFunc.query_func();
               }
            }

        };


    $("#box_id").keydown(function(event){
        event=document.all?window.event:event;
        if((event.keyCode || event.which)==13){
            btnFunc.query_func();
            return false;
        }
    });


    var iniButtonAction = function() {
        $("#query_btn").click(function () {
            btnFunc.query_func();
        });
        $("#query_wipbank_btn").click(function () {
            btnFunc.query_wipbank_func();
        });
        $("#into_btn").click(function () {
            btnFunc.bank_in_func();
        });
        $("#return_btn").click(function () {
            btnFunc.bank_out_func();
        });
        $("#refresh_btn").click(function () {
        	iniContorlData();
        	$("#box_id").val("");
        	$("#retPrdAListGrd").clearGridData();
        });
        

    };

    var iniContorlData = function(){
        toolFunc.iniWipBankSelect();

    };
    var initializationFunc = function(){
        $("#box_id").val("");
        iniButtonAction();
        iniContorlData();
    };
    initializationFunc();
});